package rs.etf.kdp.server;

import java.util.LinkedList;
import java.util.List;

/**
 * Simple list of simulations
 * @author Djordje Vukovic
 * @version 1.0
 * @created 20-May-2011 2:30:28 AM
 */
public class CurrentSimulations {

	public Heartbeater m_Heartbeater;
	private List<Simulation> activeSimulations;
	private List<Simulation> finishedSimulations;
	
	public CurrentSimulations(){
		activeSimulations = new LinkedList<Simulation>();
		finishedSimulations = new LinkedList<Simulation>();
	}
	
	public void setFinished(Simulation s, String newStatus)
	{
		activeSimulations.remove(s);
		s.setSimulationStatus(newStatus);
		finishedSimulations.add(s);
		
	}

	public List<Simulation> getActiveSimulations() {
		return activeSimulations;
	}

	public void setActiveSimulations(List<Simulation> activeSimulations) {
		this.activeSimulations = activeSimulations;
	}

	public List<Simulation> getFinishedSimulations() {
		return finishedSimulations;
	}

	public void setFinishedSimulations(List<Simulation> finishedSimulations) {
		this.finishedSimulations = finishedSimulations;
	}

	public void addActiveSimulation(Simulation s)
	{
		activeSimulations.add(s);
	}
	
	public void removeActiveSimulation(long id)
	{
		Simulation toReplace = null;
		int size = activeSimulations.size();
		for(int i=0; i<size; i++)
		{
			if(activeSimulations.get(i).getJob().getServerID()==id)
			{
				//activeSimulations.remove(i);
				toReplace = activeSimulations.get(i);
			}
		}
		
		if(toReplace==null) return;
		activeSimulations.remove(toReplace);
		finishedSimulations.add(toReplace);
	}
	
	public Simulation getActiveSimulationById(long id)
	{
		int size = activeSimulations.size();
		for(int i=0; i<size; i++)
		{
			if(activeSimulations.get(i).getJob().getServerID()==id)
			{
				return activeSimulations.get(i);
			}
		}
		return null;
	}
	
	public String getAnySimulationStatusById(long id)
	{
		for(Simulation s : activeSimulations)
		{
			if(s.getJob().getServerID()==id)
			{
				return s.getSimulationStatus();
			}
		}
		
		for(Simulation s : finishedSimulations)
		{
			if(s.getJob().getServerID()==id)
			{
				return s.getSimulationStatus();
			}
		}
		
		return null;
	}
	
	
	public Simulation getAnySimulationById(long id)
	{
		for(Simulation s : activeSimulations)
		{
			if(s.getJob().getServerID()==id)
			{
				return s;
			}
		}
		
		for(Simulation s : finishedSimulations)
		{
			if(s.getJob().getServerID()==id)
			{
				return s;
			}
		}
		
		return null;
	}

}